

//点击关闭右侧二维码显示
$('.del').on('click', fu)
function fu() { 
    $('.code-down-box').css('display','none')
}

//判断是否登录
function getName() {
    let userData = localStorage.getItem('username');
    let userShow = document.querySelector('#signin-url');
    let idShow = document.querySelector('#reg-url');
    let lkLogin = document.querySelector('.vip-price');
    

    if (userData == null) {
        userShow.innerText = '[请登录]';
        idShow.innerText = '[免费注册]';
        lkLogin.style.display = 'inline-block';
    }
    else {
        console.log(userData);
        userShow.innerText = userData;
        userShow.style.color = 'black';
        lkLogin.style.display = 'none';
        let quit = document.querySelector('#reg-url');
        quit.innerText = '[退出]';
        quit.setAttribute('href','javascript:void(0);')
    }
}
getName();

//点击退出

function exit(evt) {
    let e = evt || window.event;
    let exit = document.querySelector('#reg-url');
    console.log(exit.getAttribute('href'))
    if (exit.getAttribute('href') == 'javascript:void(0);') { 
        exit.innerText = '[免费注册]';
        e.preventDefault();
        exit.setAttribute('href', 'login.html')
        // let keys = Object.keys(localStorage);
        localStorage.removeItem('username');
        localStorage.removeItem('token')
        getName();
    }
    
}


//放大镜效果
let minShow = document.getElementsByClassName('min-img')
let moveShow = document.querySelector('.move-object');
let overShow = document.querySelector('.move-over');
console.log(minShow)
// console.log(overShow)
minShow[0].onmouseenter = function () { 
    moveShow.style.display = 'block';
    overShow.style.display = 'block';
}
minShow[0].onmouseleave = function () { 
    moveShow.style.display = 'none';
    overShow.style.display = 'none';
}

minShow[0].onmousemove = function (evt) { 
    let e = evt || window.event;

    let boxWidth = (minShow[0].offsetWidth);
    let boxHeight = (minShow[0].offsetHeight);
    let leftData = (minShow[0].getBoundingClientRect().left)
    let topData = (minShow[0].getBoundingClientRect().top)
    let maskX = e.clientX - leftData - moveShow.offsetWidth/2;
    let maskY = e.clientY - topData - moveShow.offsetHeight/2;

    if(maskX < 0){
        maskX = 0;
    }
    if(maskY < 0){
        maskY = 0;
    }
    if (maskX + moveShow.offsetWidth > boxWidth) { 
        maskX = boxWidth - moveShow.offsetWidth;
    }
    if (maskY + moveShow.offsetHeight > boxHeight) { 
        maskY = boxHeight - moveShow.offsetHeight;
    }
    moveShow.style.left = maskX + 'px';
    moveShow.style.top = maskY + 'px';

    bigImg.style.left = -(maskX + maskX * 0.56) + 'px';
    bigImg.style.top = -(maskY + maskY * 0.56) + 'px';
}


//获取到请求的URL，后发送相关连接请求获取到返回数据
let getAll;
let setUrl = new URLSearchParams(location.search)
// console.log(setUrl.toString())
let pid = (setUrl.get('id'));
$('.value-space').text(pid);
let oncGetAll = 'http://jx.xuzhixiang.top/ap/api/detail.php';
let p1 = axios.get(oncGetAll, { params: { id: pid } });
p1.then((data) => {
    console.log(data.data.data)
    getAll = data.data.data
    if (data.status == 200) {
        $('.name').text(getAll.pname);
        $('.price').text(getAll.pprice)
        console.log($('.show-img').prop('src'))
        $('.show-img').prop('src',getAll.pimg)
    }
})

// 点击选择商品颜色

$('.colors').find('li').click(function (e) { 
    console.log(e.target.parentNode.tagName)
    let na = (e.target.parentNode.tagName)
    if (na == 'LI') {
        let yuan = (e.target.parentNode);
        $('.colors').find('li').css('borderColor', '#ccc')
        yuan.style.borderColor = '#000';
    }
})

//控制数量的加减

$('.min-num').click(function () { 
    let sum = ($('#num').text());
    if (sum - 1 <= 0) {
        $('#num').text(1)
    } else { 
        $('#num').text(sum - 1)
        // $('.price').text((((getAll.pprice) * (+(sum) - 1)).toFixed(2)));
        $('.price').text((((getAll.pprice) * (+(sum) - 1)).toFixed(2)));
    }
})
$('.max-num').click(function () { 
    let sum = ($('#num').text());
    $('#num').text(+(sum) + 1);
    $('.price').text((((getAll.pprice) * (+(sum) + 1)).toFixed(2)));
})

//点击加入购物车

$('.add-to-cart').click(function () { 
    let userId = localStorage.getItem('username')
    if (userId == null) { 
        alert('请先登录或是注册');
        // location.href = ('../login.html');
        return;
    }
    let uid = localStorage.getItem("id");
    console.log('%c [ uid ]-144', 'font-size:13px; background:pink; color:#bf2c9f;', uid)
    let addUrl = 'http://jx.xuzhixiang.top/ap/api/add-product.php';
    let pnum = ($('#num').text());
    console.log('%c [ pnum ]-147', 'font-size:13px; background:pink; color:#bf2c9f;', pnum)


    axios.get(addUrl, { params: { pid, uid, pnum } }).then((r) => { 
        console.log(r.data.msg)
        if (r.data.msg == '修改成功' || r.data.msg == '插入成功') { 
            alert('加入购物车成功')
        }
    })
})

//点击收藏
function collect(evt) { 
    alert('已收藏')
}

//转换金额格式
function toMoney(num) {
    num = num.toFixed(2);
    num = parseFloat(num)
    num = num.toLocaleString();
    return num;
}